package cn.zhizihua.controller;

import cn.zhizihua.pojo.Food;
import cn.zhizihua.pojo.Order;
import cn.zhizihua.query.BaseQuery;
import cn.zhizihua.query.FoodQuery;
import cn.zhizihua.query.OrderQuery;
import cn.zhizihua.service.IOrderService;
import cn.zhizihua.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(value = "订单的API",description="订单相关的CRUD功能")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    IOrderService orderService;


    @ApiOperation(value = "添加订单")
    @PostMapping      //  请求方式 + 请求路径   post  /order
    public ResultBean add(@RequestBody Order order){
        System.out.println("----OrderController-----add--------");
        System.out.println(order);
        return orderService.add(order);
    }

    @ApiOperation(value = "更新订单")
    @PutMapping    //   put   /order
    public ResultBean update(@RequestBody Order order){
        System.out.println("-------OrderController--------update-------------");
        System.out.println(order);
        return orderService.update(order);
    }

    //  delete   /order/1003
    @ApiOperation(value = "删除订单")
    @DeleteMapping("/{id}")   // http://localhost:8080/order/1003
    public ResultBean del(@PathVariable("id") Integer id){
        System.out.println("-------OrderController--------del-------------");
        System.out.println(id);
        return orderService.delete(id);
    }

    //  get   /order/1004
    @ApiOperation(value = "通过id搜素")
    @GetMapping("/{id}")   // http://localhost:8080/order/1003
    public ResultBean getById(@PathVariable("id") Integer id){
        System.out.println("-------OrderController--------getById-------------");
        System.out.println(id);
        return orderService.getById(id);
    }

    @ApiOperation(value = "分页条件搜索")
    @PostMapping("/all")  //post /order/all
    public ResultBean getAll(@RequestBody OrderQuery orderQuery){
        System.out.println("-------BaseController--------getAll-------------");
        return orderService.getAll(orderQuery);
    }

    @ApiOperation(value = "批量删除")
    @PostMapping("/batch/{ids}")   // post + /order/batch
    public ResultBean batchDel(@PathVariable("ids") String ids){
        System.out.println("----------OrderController---------batchDel---------------");
        System.out.println(ids); //接收到的参数格式 ： 21,19
        String[] strIds = ids.split(",");  //字符串拆分  [21,19]
        System.out.println(Arrays.toString(strIds));
        List<Integer> numids = new ArrayList<>();  //创建一个Integer集合  service层需要的参数类型
        for (String strid: strIds) {
            numids.add(Integer.parseInt(strid));//将数组中的每一项转型为Integer再加入到集合中
        }
        System.out.println(numids);
        return orderService.batchDel(numids);
    }

    @ApiOperation("通过订单id获取订单详细")
    @GetMapping("/detail/{orderId}")   //get + /order/detail/1
    public ResultBean getByOrderId(@PathVariable("orderId") Integer orderId){
        System.out.println("-----OrderController------getByOrderId------");
        System.out.println(orderId);
        return orderService.getDetailsByOrderId(orderId);
    }

    @ApiOperation("通过用户id获取订单信息")
    @GetMapping("/detail/user/{userId}")   //get + /order/detail/user/1
    public ResultBean getByUserId(@PathVariable("userId") Integer userId){
        System.out.println("-----OrderController------getByUserId------");
        System.out.println(userId);
        return orderService.getOrderByUserId(userId);
    }

}